﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Smartec.Accounts.Camel.AMS.Model.Entities;
using System.Text;
using Smartec.Accounts.Camel.AMS.Model.Facades;
using Smartec.Accounts.Camel.AMS.Model.Managers;

namespace Smartec.Accounts.Camel.AMS.WebApp.Controllers
{
    public class RegisterController : Controller
    {
        //
        // GET: /Register/

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(Registered registered)
        {
            registered.Guid = Guid.NewGuid();
            var reg = RegisteredFacade.GetRegisteredByEmail(registered.Email);
            if (reg != null)
            {
                ViewData["found"] = "تم استخدام هذا البريد الالكترونى من قبل";
                return View();
            }
            else
            {
                StringBuilder str = new StringBuilder();
                str.Append("click on the link to activate your account");
                str.Append(Environment.NewLine);
                string url = Request.Url.AbsoluteUri.Replace(Request.Url.AbsolutePath, "");
                url += "/Activate/index/" + registered.Guid;
                str.Append(url);
                RegisteredFacade.AddRegestered(registered);
                RegisteredFacade.Save();
                MailServices.SendEmail(registered.Email, "Account Activation", str.ToString(), false);

                return RedirectToAction("index", "Home");
            }

        }

    }
}
